﻿/* The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. 
 * That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all 
 * related changes will be coordinated. All that the class needs is a Save method and a property for each repository. Each repository property 
 * returns a repository instance that has been instantiated using the same database context instance as the other repository instances.
 * 
 * After defining UnitOfWorks, update the controllers to use it.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using FingerPrint.WebUI.Concrete;
using FingerPrint.WebUI.Models;

namespace FingerPrint.WebUI.DAL
{
    public class UnitOfWork : IDisposable
    {

        private FingerPrintContext context = new FingerPrintContext();

        private GenericRepository<SalesOrder> salesOrderRepository;
        private GenericRepository<Employee> employeeRepository;
        private GenericRepository<Customer> customerRepository;
        private GenericRepository<Contact> contactRepository;
        private GenericRepository<Product> productRepository;
        private GenericRepository<Department> departmentRepository;
        private GenericRepository<ProductGroup> productGroupRepository;
        private GenericRepository<UserProfile> userProfileRepository;

        // Each repository property checks whether the repository already exists. If not, it instantiates the repository, 
        // passing in the context instance. As a result, all repositories share the same context instance.
        public GenericRepository<SalesOrder> SalesOrderRepository
        {
            get
            {

                if (this.salesOrderRepository == null)
                {
                    this.salesOrderRepository = new GenericRepository<SalesOrder>(context);
                }
                return salesOrderRepository;
            }
        }

        public GenericRepository<Employee> EmployeeRepository
        {
            get
            {

                if (this.employeeRepository == null)
                {
                    this.employeeRepository = new GenericRepository<Employee>(context);
                }
                return employeeRepository;
            }
        }

        public GenericRepository<UserProfile> UserProfileRepository
        {
            get
            {

                if (this.userProfileRepository == null)
                {
                    this.userProfileRepository = new GenericRepository<UserProfile>(context);
                }
                return userProfileRepository;
            }
        }

        public GenericRepository<Customer> CustomerRepository
        {
            get
            {

                if (this.customerRepository == null)
                {
                    this.customerRepository = new GenericRepository<Customer>(context);
                }
                return customerRepository;
            }
        }

        public GenericRepository<Contact> ContactRepository
        {
            get
            {

                if (this.contactRepository == null)
                {
                    this.contactRepository = new GenericRepository<Contact>(context);
                }
                return contactRepository;
            }
        }

        public GenericRepository<Department> DepartmentRepository
        {
            get
            {

                if (this.departmentRepository == null)
                {
                    this.departmentRepository = new GenericRepository<Department>(context);
                }
                return departmentRepository;
            }
        }


        public GenericRepository<Product> ProductRepository
        {
            get
            {

                if (this.productRepository == null)
                {
                    this.productRepository = new GenericRepository<Product>(context);
                }
                return productRepository;
            }
        }


        public GenericRepository<ProductGroup> ProductGroupRepository
        {
            get
            {

                if (this.productGroupRepository == null)
                {
                    this.productGroupRepository = new GenericRepository<ProductGroup>(context);
                }
                return productGroupRepository;
            }
        }


        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}